﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Xml.Serialization;

namespace Map
{
    [Serializable]
    public static class Settings
    {
        internal static readonly string mMapSettingsPath = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath);
        
        // Areas that will be preserved in file cache permanently
        [XmlElement]
        internal static Dictionary<int, Rectangle> _PreserveAreas = new Dictionary<int, Rectangle>();

        // Memory cache interval in milliseconds (15 seconds by default)
        private static int _CacheInterval = 15000;
        // File cache interval in timespan (2 days by default)
        private static TimeSpan _PersistentInterval = new TimeSpan(2, 0, 0, 0);
        // File path for tile cache
        private static string _CachePath = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath);

        public static int CacheInterval { get { return _CacheInterval; } set { _CacheInterval = value; } }
        public static TimeSpan PersistentInterval { get { return _PersistentInterval; } set { _PersistentInterval = value; } }
        public static string CachePath { get { return _CachePath; } set { _CachePath = value; } }

        public static bool IsPreservable(TileClient.Key key)
        {
            return true;
        }

        public static void Load()
        {
            if (File.Exists(mMapSettingsPath))
            {
                try
                {
                    using (FileStream fs = new FileStream(mMapSettingsPath, FileMode.Open))
                    {
                       // this = (Settings)(new XmlSerializer(typeof(Settings)).Deserialize(fs));
                    }
                }
                catch (InvalidOperationException x)
                {
                    System.Diagnostics.Debug.WriteLine(x.Message);
                }
            }
        }

        public static void Save()
        {
            try
            {
                using (FileStream fs = new FileStream(mMapSettingsPath, FileMode.Create))
                {
                //    new XmlSerializer(typeof(Settings)).Serialize(fs, this);
                }
            }
            catch (Exception x)
            {
                System.Windows.Forms.MessageBox.Show(x.Message);
            }
        }
    }
}
